package demo;
/**
 * Collections 是操作集合的工具类
 * Collections.sort(arrayList);  对集合进行排序
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

class Students implements Comparable<Students>{
    private String name;
    private int age;
    private double score;

    public Students(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Students{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    @Override
    public int compareTo(Students o) {
        return (int)(this.score-o.score);
    }
}
public class Test3 {
    public static void main(String[] args) {
        ArrayList<Students> arrayList = new ArrayList<>();
        arrayList.add(new Students("hello",18,90.8));
        arrayList.add(new Students("world",20,78.9));
        arrayList.add(new Students("hhh",19,100));
        //System.out.println(arrayList);//通过循环，对顺序表中的每一个元素进行输出
        int index = arrayList.size();
        for (int i = 0; i < index; i++) {
            System.out.print(arrayList.get(i)+" ");
        }
        System.out.println();
        /**
         * 排序
         */
        Collections.sort(arrayList);
        for (Students x:arrayList) {
            System.out.println(x);
        }
    }
}
